package com.rybbaby.tss.front.tssAppActivity.service.imp;

import com.rybbaby.tss.core.utils.StringUtils;
import com.rybbaby.tss.core.web.service.imp.BaseServiceImp;
import com.rybbaby.tss.front.tssAppActivity.entity.TssAppActivity;
import com.rybbaby.tss.front.tssAppActivity.service.ITssAppActivityService;
import com.rybbaby.tss.platform.sysAdminUser.entity.SysAdminUser;
import com.rybbaby.tss.platform.sysFiles.service.ISysFilesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Table;
import java.io.Serializable;
import java.lang.reflect.Field;

/**
 * @description 活动数据库操作服务实现
 * 
 * @author zhangligang
 * 
 * @createDate 2018-01-03 上午9:52:34
 */
@Service
@Transactional
public class TssAppActivityServiceImp extends BaseServiceImp<TssAppActivity> implements ITssAppActivityService {
    @Autowired
    private ISysFilesService sysFilesService;
    @Override
    public Serializable save(TssAppActivity entity, String sessionId, String loginUserId, String... fileRelevances) throws Exception {
        if(entity == null) {
            return null;
        }
        Serializable businessId = save(entity);
        if(businessId != null) {
            if(fileRelevances != null && fileRelevances.length > 0) {
                Table table = TssAppActivity.class.getAnnotation(Table.class);
                String businessTableName = table.name();
                for(String fileRelevance : fileRelevances) {
                    if(StringUtils.isNotBlank(fileRelevance)) {
                        if(!fileRelevance.contains("=>")) {
                            continue;
                        }
                        String[] fileReleVanceInfo = fileRelevance.split("=>");
                        String groupName = fileReleVanceInfo[0];
                        String fieldName = fileReleVanceInfo[1];
                        if(!sysFilesService.useFile(businessTableName, "id", businessId, fieldName, groupName, sessionId, loginUserId, null)) {
                            throw new Exception("使用文件失败!");
                        }
                    }
                }
            }
        }
        return businessId;
    }
    @Override
    public void updateEntitiy(TssAppActivity entity, String sessionId, String loginUserId, String delteFileIds, String... fileRelevances) throws Exception {
        if(entity != null && entity.getId() != null) {
            saveOrUpdate(entity);
            Class<? extends TssAppActivity> cls = entity.getClass();
            if(fileRelevances != null && fileRelevances.length > 0) {
                Table table = SysAdminUser.class.getAnnotation(Table.class);
                String businessTableName = table.name();
                for(String fileRelevance : fileRelevances) {
                    if(StringUtils.isNotBlank(fileRelevance)) {
                        if(!fileRelevance.contains("=>")) {
                            continue;
                        }
                        String[] fileReleVanceInfo = fileRelevance.split("=>");
                        String groupName = fileReleVanceInfo[0];
                        String fieldName = fileReleVanceInfo[1];
                        Field declaredField = cls.getDeclaredField(fieldName);
                        declaredField.setAccessible(true);
                        Object oldValue = declaredField.get(entity);
                        if(!sysFilesService.updateUsedFile(businessTableName, "id", entity.getId(), fieldName, groupName, sessionId, loginUserId, delteFileIds, oldValue + "")) {
                            throw new Exception("使用文件失败!");
                        }
                    }
                }
            }
        }
    }
}
